
$(function(){

	function dialogo(rowsSelected){
		if(rowsSelected=='_'){
			accion='add';
		}else{
			accion='edit';
		}
		//var bodyForm    = $('form', 'div#dialog-form-add').html();
		data=[];
		var bodyForm    = '';

		$.ajax({
			type: "POST",
			url: 'empresa/datos/principal',
			async:false,
			data:data,
			success: function(msg){
				bodyForm=msg;

			}
		});
//window.console.log(bodyForm);

		var propiedades={
			width : 800,
			height: 500,
			show  : 'scale',
			hide  : 'scale',
			collapsingAnimation:
			{
				animated: "clip",
				duration: 300,
				easing: "easeOutExpo"
			},
			expandingAnimation: {
				animated: "clip",
				duration: 300,
				easing: "easeOutExpo"
			}
		};

		idDialog = $("table#filtro-<?php echo $this->pre ?>").jqGrid('getDialogsForms', accion, rowsSelected, bodyForm, "<?php echo $this->lang->line('FORM_ADD_TITLE') ?>",propiedades);
		////$('div#'+idDialog+' #nacimiento').datepicker({ dateFormat: 'dd/mm/yy' });
		////$('div#'+idDialog+' #fcolocado').datepicker({ dateFormat: 'dd/mm/yy' });
		//$('#'+idDialog+' #tabs-<?php echo $this->pre ?>'+' #div-<?php echo $this->pre ?>-2'+' #table-<?php echo $this->pre ?>-2'+' #fregistro').datepicker({ dateFormat: 'dd/mm/yy' });
        //
		//$('div#'+idDialog+'  #tabs-<?php echo $this->pre ?>').tabs();
		return idDialog;
	}

	jQuery("#filtro-<?php echo $this->pre ?>").jqGrid({
	    url:'<?php echo site_url($this->url."grid") ?>',
	    datatype: "json",
            colNames:[
                "<?php echo $this->lang->line('G_ID')                            ?>",
				"<?php echo $this->lang->line('G_CIF')                           ?>",
				"<?php echo $this->lang->line('G_NIF')                           ?>",
				"<?php echo $this->lang->line('G_NOMBRE_COM')                    ?>",
				"<?php echo $this->lang->line('G_RAZON_SOC')                     ?>",
				"<?php echo $this->lang->line('G_TELEFONO')                      ?>",
				"<?php echo $this->lang->line('G_FAX')                           ?>",
				"<?php echo $this->lang->line('G_WEB')                           ?>",
				"<?php echo $this->lang->line('G_CONTACTO')                      ?>",
				"<?php echo $this->lang->line('G_TELEFONO2')                     ?>",
				"<?php echo $this->lang->line('G_MOVIL')                         ?>",
				"<?php echo $this->lang->line('G_EMAIL_CON')                     ?>",
            ],
	    colModel:[
	        {
				name          :'id'                     ,
				index         :'id'                     ,
				width         :20                       ,
				key           :true                     ,
				hidden        :true                     ,
			},
			{
				name          :'cif'                    ,
				index         :'cif'                    ,
				width         :20                       ,
			},
			{
				name          :'nif'                    ,
				index         :'nif'                    ,
				width         :20                       ,
			},
			{
				name          :'nombre_com'             ,
				index         :'nombre_com'             ,
				width         :20                       ,
			},
			{
				name          :'razon_soc'              ,
				index         :'razon_soc'              ,
				width         :20                       ,
			},
			{
				name          :'telefono'               ,
				index         :'telefono'               ,
				width         :20                       ,
			},
			{
				name          :'fax'                    ,
				index         :'fax'                    ,
				width         :20                       ,
			},
			{
				name          :'web'                    ,
				index         :'web'                    ,
				width         :20                       ,
			},
			{
				name          :'contacto'               ,
				index         :'contacto'               ,
				width         :20                       ,
			},
			{
				name          :'telefono2'              ,
				index         :'telefono2'              ,
				width         :20                       ,
			},
			{
				name          :'movil'                  ,
				index         :'movil'                  ,
				width         :20                       ,
			},
			{
				name          :'email_con'              ,
				index         :'email_con'              ,
				width         :20                       ,
			},
	    ],
	    rowNum       : 10,
	    width        : 900,
	    height       : 200,
	    rowList      : [10,20,30],
	    pager        : '#barra-<?php echo $this->pre ?>',
	    sortname     : 'id',
	    viewrecords  : true,
	    sortorder    : "asc",
	    caption      : "<?php echo $this->lang->line('G_CAPTION') ?>",
		multiselect  : true,
		forceFit     : true,
		rownumbers   : true,
		sortable     : true,
		ondblClickRow:function(rowid,iCol, cellcontent,e){
			//var rowsSelected=rowid;
			//dialogo(rowsSelected);
		}
	});

	jQuery("#filtro-<?php echo $this->pre ?>").jqGrid(
        'navGrid',
        '#barra-<?php echo $this->pre ?>',
        {
	    edit:false,
	    add :false,
	    del :false,
	    view:true
        },
        {},
        {},
        {},
        {
	    multipleSearch:true,
	    multipleGroup :true,
	    showQuery     :true
        }
    ).jqGrid(
        'navButtonAdd',
        "#barra-<?php echo $this->pre ?>",
        {
            caption:"",
            buttonicon:"ui-icon-trash",
            onClickButton: function(){
				var rowsSelected = $("table#filtro-<?php echo $this->pre ?>").getGridParam('selarrrow');
				$("body").append('<div id="dialog-confirm">Esta Seguro de Eliminar los items seleccionados</div>');
				data='ids='+rowsSelected;
				$( "#dialog-confirm" ).dialog({
					resizable: false,
					modal: true,
					width:500,
					buttons: {
						"Eliminar todos los items": function() {
							hace_post('empresa/datos/eliminar',data,true);
							jQuery("#filtro-<?php echo $this->pre ?>").trigger("reloadGrid");
							$( this ).dialog( "close" );
						},
						Cancel: function() {
							$( this ).dialog( "close" );
						}
					}
				});
            },
            position:"first"
        }
    ).jqGrid(
        'navButtonAdd',
        "#barra-<?php echo $this->pre ?>",
        {
            caption:"",
            buttonicon:"ui-icon-pencil",
            onClickButton: function(){
				var rowsSelected = $("table#filtro-<?php echo $this->pre ?>").getGridParam('selarrrow');
				dialogo(rowsSelected);
            },
            position:"first"
        }
    ).jqGrid(
        'navButtonAdd',
        "#barra-<?php echo $this->pre ?>",
        {
            caption:"",
            buttonicon:"ui-icon-plus",
            onClickButton: function(){
				var rowsSelected='_';
                idDialog=dialogo(rowsSelected);
                editar(idDialog+' #edit1');
             },
            position:"first"
        }
    ).jqGrid(
        'sortableRows'
    ).jqGrid(
        'gridResize'
    );

})
